package br.com.dallas.usuario.manager;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import br.com.dallas.parametro.javabean.ParametroBean;
import br.com.dallas.usuario.controller.UsuarioController;
import br.com.dallas.usuario.javabean.UsuarioBean;
import br.com.dallas.util.exception.AlterarObjetoException;
import br.com.dallas.util.exception.ClasseNaoEncontradaException;
import br.com.dallas.util.exception.ConexaoException;
import br.com.dallas.util.exception.ConsultarObjetoException;
import br.com.dallas.util.exception.GerarSequenceException;
import br.com.dallas.util.exception.InserirObjetoException;
import br.com.dallas.util.exception.ListarObjetoException;
import br.com.dallas.util.exception.UsuarioException;

@ManagedBean(name="usuarioManagerBean")
@ViewScoped
public class UsuarioManagerBean implements Serializable
{
	private static final long serialVersionUID = 1L;
	private UsuarioBean usuarioBean;
	private List<UsuarioBean> usuarios;
	
	public UsuarioBean getUsuarioBean()
	{
		if (usuarioBean == null) usuarioBean = new UsuarioBean();
		
		return usuarioBean;
	}
	
	public void setUsuarioBean(UsuarioBean usuarioBean)
	{
		this.usuarioBean = usuarioBean;
	}
	
	public List<UsuarioBean> getUsuarios()
	{
		UsuarioController usuarioController = null;
		try
		{
			if (this.usuarios == null)
			{
				usuarioController = new UsuarioController();
				this.usuarios = usuarioController.listaUsuarios(getUsuarioBean());
			}
		}
		catch (ConexaoException e)
		{
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(e.getMessage()));
		}
		catch (ClasseNaoEncontradaException e)
		{
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(e.getMessage()));
		}
		catch (ListarObjetoException e)
		{
			FacesContext.getCurrentInstance().addMessage(null,
					new FacesMessage(e.getMessage()));
		}
		finally
		{
			if (usuarioController != null)
			{
				usuarioController.close();
				usuarioController = null;
			}
		}
		return usuarios;
	}
	
	@PostConstruct
	public void init()
	{
		//usuarioBean = new UsuarioBean();
	}
	
	public void incluir()
	{
		UsuarioController usuarioController = null;
		try
		{
			usuarioController = new UsuarioController();
			usuarioController.inserirUsuario(usuarioBean);
			FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/view/dispatcher/SucessoAdministrador.jsf");
		}
		catch (IOException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (ConexaoException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (ClasseNaoEncontradaException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (InserirObjetoException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (GerarSequenceException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (ConsultarObjetoException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (UsuarioException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		finally
		{
			if (usuarioController != null)
			{
				usuarioController.close();
				usuarioController = null;
			}
				
		}
	}
	
	public void alterar()
	{
		UsuarioController usuarioController = null;
		try
		{
			usuarioController = new UsuarioController();
			usuarioController.alterarUsuario(usuarioBean);
			FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/view/dispatcher/SucessoAdministrador.jsf");
		}
		catch (IOException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (ConexaoException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (ClasseNaoEncontradaException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (ConsultarObjetoException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (UsuarioException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (AlterarObjetoException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		finally
		{
			if (usuarioController != null)
			{
				usuarioController.close();
				usuarioController = null;
			}
				
		}
	}
	
	public void alterarSenha()
	{
		HttpServletRequest request = null;
		HttpSession session = null;
		UsuarioBean usuarioBean = null;
		UsuarioController usuarioController = null;
		try
		{
			request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
			session = (HttpSession) request.getSession();
			usuarioBean = (UsuarioBean)session.getAttribute("usuarioLogado");
			
			if (usuarioBean != null)
			{
				usuarioController = new UsuarioController();
				usuarioController.alterarUsuario(usuarioBean);
				FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/view/dispatcher/Sucesso.jsf");
			}
		}
		catch (IOException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (ConexaoException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (ClasseNaoEncontradaException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (ConsultarObjetoException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (UsuarioException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (AlterarObjetoException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		finally
		{
			if (usuarioController != null)
			{
				usuarioController.close();
				usuarioController = null;
			}
			
			if (request != null)
				request = null;
			
			if (session != null)
				session = null;
		}
	}
	
	public void logar()
	{
		UsuarioController usuarioController = null;
		ParametroBean parametroBean = null;
		HttpSession session = null;
		try
		{
			usuarioController = new UsuarioController();
			parametroBean = new ParametroBean();
			usuarioController.logarSistema(usuarioBean, parametroBean);
			session =  (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			session.setAttribute("usuarioLogado", usuarioBean);
			if (usuarioBean.getTipo().equals("A"))
			{
				FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/view/PainelAdministrador.jsf");
			}	
			else
			{
				session.setAttribute("parametroLogado", parametroBean);
				FacesContext.getCurrentInstance().getExternalContext().redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/view/Principal.jsf");
			}	
		}
		catch(ConexaoException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (ConsultarObjetoException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (UsuarioException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (ClasseNaoEncontradaException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		catch (IOException e)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
		}
		finally
		{
			if (usuarioController != null)
			{
				usuarioController.close();
				usuarioController = null;
			}
			
			if (session != null)
				session = null;
		}
	}
}
